package com.treasure.mall.zbqmallapi.controller;

import com.treasure.mall.biz.dto.condition.AuctionSessionCondition;
import com.treasure.mall.biz.entity.auction.AuctionBidLogPO;
import com.treasure.mall.biz.entity.auction.AuctionBidPO;
import com.treasure.mall.biz.entity.auction.AuctionSessionPO;
import com.treasure.mall.biz.entity.product.ProductImgPO;
import com.treasure.mall.biz.entity.user.UserPO;
import com.treasure.mall.biz.repository.product.ProductImgRepository;
import com.treasure.mall.biz.service.core.auction.AuctionBidService;
import com.treasure.mall.biz.service.core.auction.AuctionSessionService;
import com.treasure.mall.biz.vo.PageDataVO;
import com.treasure.mall.zbq.common.annotation.CurrentUser;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbqmallapi.annotation.AccessControl;
import com.treasure.mall.zbqmallapi.vo.auction.*;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/7/17
 */
@Api(tags = "拍卖模块")
@RestController
@RequestMapping("/auction")
public class AuctionController {
    @Autowired
    private AuctionSessionService auctionSessionService;
    @Autowired
    private ProductImgRepository productImgRepository;
    @Autowired
    private AuctionBidService auctionBidService;

    @GetMapping("/session/list")
    public ApiResult list(
            AuctionSessionCondition condition,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size
    ) {
        Page<AuctionSessionPO> sessionPOS = auctionSessionService.findAll(page, size, condition);

        PageDataVO<AuctionSessionVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageCount(sessionPOS.getTotalPages());
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageSize(size);
        pageDataVO.setTotal(sessionPOS.getTotalElements());
        List<AuctionSessionVO> sessionVOS = new ArrayList<>();
        sessionPOS.getContent().forEach(p -> sessionVOS.add(new AuctionSessionVO(p)));
        pageDataVO.setRows(sessionVOS);

        return ApiResult.ok(pageDataVO);
    }

    @GetMapping("/session/detail")
    public ApiResult detail(Long sessionId) {
        AuctionSessionPO po = auctionSessionService.findById(sessionId);

        AuctionSessionDetailVO vo = new AuctionSessionDetailVO(po);
        List<ProductImgPO> imgPOList = productImgRepository.findAllByProductIdOrderBySortNoAsc(po.getProductPO().getProductId());
        List<String> picList = new ArrayList<>();
        imgPOList.forEach(x -> picList.add(x.getBig()));
        vo.getProduct().setPicList(picList);

        return ApiResult.ok(vo);
    }

    @GetMapping("/session/bidRecord")
    public ApiResult bidRecord(
            Long sessionId,
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int size
    ) {
        Page<AuctionBidPO> auctionBidPOS = auctionBidService.findBySessionId(sessionId, page, size);

        PageDataVO<AuctionBidVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setTotal(auctionBidPOS.getTotalElements());
        pageDataVO.setPageSize(size);
        pageDataVO.setPageIndex(page);
        pageDataVO.setPageCount(auctionBidPOS.getTotalPages());
        List<AuctionBidVO> auctionBidVOS = new ArrayList<>();
        auctionBidPOS.getContent().forEach(p -> auctionBidVOS.add(new AuctionBidVO(p)));
        pageDataVO.setRows(auctionBidVOS);

        return ApiResult.ok(pageDataVO);
    }

    @PostMapping("/session/bid")
    @AccessControl(needLogin = true)
    public ApiResult bid(
            @CurrentUser UserPO userPO,
            Long sessionId
    ) {
        AuctionSessionPO sessionPO = auctionSessionService.bid(userPO, sessionId);

        return ApiResult.ok(new AuctionSessionBaseVO(sessionPO));
    }

    @PostMapping("/session/enableBid")
    @AccessControl(needLogin = true)
    public ApiResult enableBid(@CurrentUser UserPO userPO, Long sessionId) {
        return ApiResult.ok(auctionSessionService.enableBid(userPO.getUserId(), sessionId));
    }

    @PostMapping("/session/securityDeposit")
    @AccessControl(needLogin = true)
    public ApiResult securityDeposit(@CurrentUser UserPO userPO, Long sessionId) {
        return ApiResult.ok(auctionSessionService.securityDeposit(userPO.getUserId(), sessionId));
    }

    @GetMapping("/session/bidLogs")
    public ApiResult bidLogs(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "20") int size,
            long sessionId
    ) {
        Page<AuctionBidLogPO> bidLogPOS = auctionSessionService.bidLogs(page, size, sessionId);
        PageDataVO<AuctionBidLogVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setPageSize(size);
        pageDataVO.setPageIndex(page);
        pageDataVO.setTotal(bidLogPOS.getTotalElements());
        pageDataVO.setPageCount(bidLogPOS.getTotalPages());
        List<AuctionBidLogVO> logVOS = new ArrayList<>();
        bidLogPOS.getContent().forEach(p -> logVOS.add(new AuctionBidLogVO(p)));
        pageDataVO.setRows(logVOS);

        return ApiResult.ok(pageDataVO);
    }
}
